import fp from 'fastify-plugin';

declare module 'fastify' {
    interface FastifyInstance {
        json: (data: any) => Object;
    }
}

export default fp(function (fastify, opts, done) {
    fastify.decorate('json', (data) => {
        return({
            code: 200,
            error: null,
            message: 'Success',
            data: data
        });
    });

    fastify.addHook('onSend', (request, reply, payload, done) => {
        if (reply.statusCode !== 200) {
            return done(null, payload);
        }
        done(null, payload);
    });

    done();
});
